package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.SpecialDiscount;

public class SpecialDiscountLogic extends AbstractBL{
	
	public SpecialDiscount GetSpecialDiscount(long id)
	{
		Key key = KeyFactory.createKey(EntityName.SpecialDiscount, id);
		try {
			Entity lang = datastore.get(key);
			SpecialDiscount c = new SpecialDiscount();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public SpecialDiscount GetSpecialDiscount(long hotelId,String title)
	{
		Query q = new Query(EntityName.SpecialDiscount);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterTitle);
		
		q.setFilter(filter);
		
		return GetSpecialDiscount(datastore.prepare(q).asSingleEntity());
	}
	
	public List<SpecialDiscount> GetAllSpecialDiscount(long hotelId)
	{
		Query q = new Query(EntityName.SpecialDiscount);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,"true");
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,"true");

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public SpecialDiscount GetSpecialDiscount(Entity entity)
	{
		SpecialDiscount item = new SpecialDiscount();
		item.setEntity(entity);
		return item;
	}
	
	public List<SpecialDiscount> GetList(List<Entity> entities)
	{
		List<SpecialDiscount> list = new ArrayList<SpecialDiscount>();
		for(Entity e : entities)
		{
			SpecialDiscount a = new SpecialDiscount();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public SpecialDiscount AddSpecialDiscount(long hotelId,String title,String description,Double discount,String requirement,boolean isPublish) throws EntityNotFoundException{
		SpecialDiscount t = new SpecialDiscount();
		t.setHotelId(hotelId);
		t.setTitle(title);
		t.setDescription(description);
		t.setPublish(isPublish);
		t.setRequirement(requirement);
		t.setDiscount(discount);
		t.setAvaliable(true);
		t.setStatus(true);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public SpecialDiscount EditSpecialDiscount(long specialdiscountId,String title,String description,Double discount,String requirement,boolean isPublish) throws EntityNotFoundException{
		SpecialDiscount t = GetSpecialDiscount(specialdiscountId);
		t.setTitle(title);
		t.setDescription(description);
		t.setPublish(isPublish);
		t.setRequirement(requirement);
		t.setDiscount(discount);
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public SpecialDiscount DeleteSpecialDiscount(long SpecialDiscountId) throws EntityNotFoundException{
		SpecialDiscount t = GetSpecialDiscount(SpecialDiscountId);
		t.setAvaliable(false);
		t.setStatus(false);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public SpecialDiscount ChangeStatus(long SpecialDiscountId) throws EntityNotFoundException{
		
		SpecialDiscount a = GetSpecialDiscount(SpecialDiscountId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
}
